package com.yueya.basic.news.subject.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.yueya.basic.news.subject.model.Subject;
import com.yueya.common.model.Tree;
import com.yueya.common.service.BaseService;
import com.yueya.common.util.DateUtil;
import com.yueya.common.util.UUIDGenerator;

public class SubjectService extends BaseService {

	public List<Subject> getByParentId(String pid) {
		String sql = "select * from t_subject where parent_id = '" + pid + "' and del_flag = '0' order by order_id asc";
		return Subject.dao.find(sql);
	}

	public int getDeptNumByParentId(String pid) {
		String sql = "select subject_id from t_subject where parent_id = '" + pid + "' and del_flag = '0'";
		List<Record> list = Db.find(sql);
		if (list != null && list.size() > 0) {
			return list.size();
		}
		return 0;
	}

	public Page<Record> pagination(int pageNumber, int pageSize, Map<String, Object> map) {

		String select = "select d.*,i.subject_name as parent_name ";
		StringBuffer sb = new StringBuffer(
				" from t_subject d left join t_subject i on i.subject_id=d.parent_id where d.del_flag = '0' and d.subject_id!='000'  ");

		if (!"".equals(map.get("d.level")) && map.get("d.level") != null) {
			sb.append(" and d.level = " + map.get("d.level"));
		}

		if (!"".equals(map.get("parentid")) && map.get("parentid") != null) {
			sb.append(" and d.parent_id = '" + map.get("parentid") + "'");
		}
		if (!"".equals(map.get("sid")) && map.get("sid") != null) {
			sb.append(" or d.subject_id = '" + map.get("sid") + "'");
		}
		if (!"".equals(map.get("name")) && map.get("name") != null) {
			sb.append(" and  d.subject_name like '%" + map.get("name") + "%'");
		}

		if (map.get("order_name") != null) {
			sb.append(" order by d." + map.get("order_name") + " " + map.get("order"));
		} else {
			sb.append(" order by d.order_id asc,d.create_time asc");
		}

		String sql = sb.toString();
		Page<Record> page = Db.paginate(pageNumber, pageSize, select, sql);

		return page;

	}

	@Before(Tx.class)
	public boolean save(Subject subject) {
		boolean bl = false;
		String id = subject.getStr("subject_id");
		String currentDate = DateUtil.getDate(DateUtil.timeFormat);
		if (id == null) {
			id = UUIDGenerator.getUUID();
			subject.set("subject_id", id);
			subject.set("create_time", currentDate);
			int parentDeptLevel = getLevel(subject.getStr("parent_id"));
			subject.set("level", parentDeptLevel + 1);
			int maxOrderId = getMaxOrder(subject.getStr("parent_id"));
			subject.set("order_id", maxOrderId + 1);
			bl = subject.save();
			if (!bl) {
				return bl;
			}
		} else {
			subject.set("update_time", currentDate);
			int parentDeptLevel = getLevel(subject.getStr("parent_id"));
			subject.set("level", parentDeptLevel + 1);
			bl = subject.update();
			if (!bl) {
				return bl;
			}
		}

		return bl;
	}

	public int getMaxOrder(String pid) {
		String sql = "select MAX(order_id) as max_order_id from t_subject where parent_id = '" + pid + "'";
		Subject subject = Subject.dao.findFirst(sql);
		int max = 0;
		if (subject.get("max_order_id") != null) {
			// max = dept.getBigDecimal("max_order_id").intValue();
			max = Integer.valueOf(subject.getStr("max_order_id"));
		}

		return max;
	}

	public int getLevel(String pid) {
		String sql = "select MAX(level) as dept_level from t_subject where subject_id = '" + pid + "'";
		Subject subject = Subject.dao.findFirst(sql);
		int max = 0;
		if (subject.get("level") != null) {
			// max = dept.getBigDecimal("max_order_id").intValue();
			max = subject.getInt("level");
		}

		return max;
	}

	public boolean isleaf(String pid) {
		return getDeptNumByParentId(pid) == 0;
	}

	public Tree getTreeNode(String cid) {
		Subject category = Subject.dao.findFirst(
				" select subject_id,subject_name,parent_id from  t_subject where subject_id = '" + cid + "' ");
		Tree root = new Tree();
		root.setId(category.getStr("subject_id"));
		if (cid == "000") {
			root.setIconCls("icon-wlkj-organ");
		} else {
			root.setIconCls("icon-wlkj-dept");
			if (Subject.service.isleaf(cid)) {
				root.setState("open");
			} else {
				root.setState("closed");
			}

		}

		root.setPid(category.getStr("parent_id"));
		root.setText(category.getStr("subject_name"));

		return root;
	}

	public List<Tree> queryTreeNode(String pid) {
		List<Subject> list = Subject.dao
				.find(" select subject_id,subject_name,parent_id from  t_subject where del_flag='0' and parent_id = '"
						+ pid + "' order by order_id asc");
		List<Tree> treeList = null;
		Tree root = null;
		Subject d = null;
		if (list != null && list.size() > 0) {
			treeList = new ArrayList<Tree>();
			for (int i = 0; i < list.size(); i++) {
				root = new Tree();
				d = (Subject) list.get(i);
				root.setId(d.getStr("subject_id"));
				root.setIconCls("icon-wlkj-dept");
				root.setPid(d.getStr("parent_id"));
				root.setText(d.getStr("subject_name"));
				treeList.add(root);
			}
		}

		return treeList;
	}

	public Tree recursiveTree(String cid) {
		Tree node = getTreeNode(cid);
		List<Tree> childTreeNodes = queryTreeNode(cid);
		// System.out.print(childTreeNodes.size());
		// 遍历子节点
		if (childTreeNodes != null && childTreeNodes.size() > 0) {
			for (Tree child : childTreeNodes) {
				Tree n = recursiveTree(child.getId()); // 递归
				node.getChildren().add(n);
			}
		}

		return node;
	}

	public List<Subject> getList() {
		String sql = "select * from t_subject where del_flag = '0' and level='1' order by order_id";
		return Subject.dao.find(sql);
	}
}
